

package projectx.web.base;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import projectx.core.base.ProjectXException;
import projectx.core.base.ProjectXMessage;
import projectx.core.base.ProjectXProperties;
import projectx.web.base.navi.Function;

public class ProjectXForm extends ActionForm {

    //--------------------------------------------------------------------------
    //   S T A T I C   M E M B E R S
    //
    //--------------------------------------------------------------------------
	
    private static final String _autoUser = ProjectXProperties.getProperty( "auto.remoteuser", null );


    //--------------------------------------------------------------------------
    //   S T A T I C   M E T H O D S
    //
    //--------------------------------------------------------------------------

	protected static String getUserId( HttpServletRequest request ){

        String  userId = request.getRemoteUser();

        if ( userId == null ){
            userId = _autoUser;
            System.err.println("_autoUser="+_autoUser);
        }

        return userId;
    }

    protected static void addError( ActionErrors errors, ProjectXMessage message ){

        if ( message != null ){
           addError( errors, message.getMessage() );
        }
    }

    protected static void addError( ActionErrors errors, ProjectXException exception ){

        if ( exception != null ){
           addError( errors, exception.getMessage() );
        }
    }

    static boolean isPostMethod( HttpServletRequest request ){

        return request.getMethod().equalsIgnoreCase( "POST" ) ? true : false;
    }

    private static void addError( ActionErrors errors, String message ){

        errors.add( ActionErrors.GLOBAL_ERROR, new ActionError( "errors.polaris", message ) );
    }

    static void logDebug( Logger log, String userId, Object msg ){

        if ( log.isDebugEnabled() ){
            log.debug( createMessage( userId, msg ) );
        }
    }

    static void logInfo ( Logger log, String userId, Object msg ){ log.info ( createMessage( userId, msg ) ); }
    static void logWarn ( Logger log, String userId, Object msg ){ log.warn ( createMessage( userId, msg ) ); }
    static void logError( Logger log, String userId, Object msg ){ log.error( createMessage( userId, msg ) ); }
    static void logFatal( Logger log, String userId, Object msg ){ log.fatal( createMessage( userId, msg ) ); }

    static void logError( Logger log, String userId, Object msg, Throwable t ){ log.error( createMessage( userId, msg ), t ); }
    static void logFatal( Logger log, String userId, Object msg, Throwable t ){ log.fatal( createMessage( userId, msg ), t ); }


    private static String createMessage( String userId, Object message ){

        return "[User:" + userId + "] " + message;
    }


    //--------------------------------------------------------------------------
    //   M E M B E R S
    //
    //--------------------------------------------------------------------------
    protected transient final Logger  _log = Logger.getLogger( getClass().getName() );

    private String  _funcId;


    //--------------------------------------------------------------------------
    //   M E T H O D S
    //
    //--------------------------------------------------------------------------
  
    public void setFuncId( String funcId ){ _funcId = funcId; }

    public String getFuncId(){ return _funcId; }

    public final ActionErrors validate( ActionMapping mapping, HttpServletRequest request ){

        String  userId = getUserId( request );

        setFuncId( request );

        if ( !isPostMethod( request ) || _funcId == null ){
            return null;
        }

        ActionErrors    errors = new ActionErrors();

        validate( errors, mapping, request );

        if ( errors.isEmpty() ){
            logDebug( userId, this );
        }
        else {
            logInfo( userId, "Error found: " + this );
        }

        return errors;
    }

    private void setFuncId( HttpServletRequest request ){

        if ( _funcId == null ){

            Function    function = (Function)request.getSession().getAttribute( "keep.function" );

            _funcId = ( function == null ) ? null : function.getId();
        }
    }

    protected void validate( ActionErrors errors, ActionMapping mapping, HttpServletRequest request ){

        // nop
    }

    protected ProjectXMessage checkMandatory( String name, String value ){

        return ( value == null || value.length() == 0 ) ? new ProjectXMessage( "E0311", name ) : null;
    }

    protected ProjectXMessage checkSelected( String name, String value ){

        return ( value == null || value.length() == 0 ) ? new ProjectXMessage( "E0312", name ) : null;
    }

    protected ProjectXMessage checkSelected( String name, Object[] value ){

        return ( value == null || value.length == 0 ) ? new ProjectXMessage( "E0312", name ) : null;
    }

    protected void logDebug( String userId, Object msg ){ if ( _log != null ) logDebug( _log, userId, msg ); }
    protected void logInfo ( String userId, Object msg ){ if ( _log != null ) logInfo ( _log, userId, msg ); }
    protected void logWarn ( String userId, Object msg ){ if ( _log != null ) logWarn ( _log, userId, msg ); }
    protected void logError( String userId, Object msg ){ if ( _log != null ) logError( _log, userId, msg ); }
    protected void logFatal( String userId, Object msg ){ if ( _log != null ) logFatal( _log, userId, msg ); }

    protected void logError( String userId, Object msg, Throwable t ){ if ( _log != null ) logError( _log, userId, msg, t ); }
    protected void logFatal( String userId, Object msg, Throwable t ){ if ( _log != null ) logFatal( _log, userId, msg, t ); }

    public String toString(){

        return "FuncId[" + _funcId + "]";
    }
    
    protected static void addError( ActionErrors errors, ProjectXMessage message, String multiRowMsg ){

        if ( message != null ){
        	addError( errors, message.getMessage( multiRowMsg ) );
        }
    }
}
